apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../dialog"], function(V, $api, $, app, service, dialog) {
      // app.ready();
        var vm = {
            el: '.container',
            data: {
                activeIndex: 0,
                activeUpIndex: 0,
                orderNumber: {
                    unpay_num: '',
                    pay_num: '',
                    express_num: '',
                    refund_num: '',
                    comment_num: '',
                    finish_num: ''
                },
                orderList: [],
                order_state: 1,
                page: 1,
                shopId: '',
                tem_order_id: '',
                refundPrice: '',
                order_return_id: '',
                yzc_status: '',
                deal_confirm: '',
                isOpenFrame: false
            },
            mounted: function() {
              var that = this;

              $api.addEvt($api.dom('#btn-back'), 'click', function() {
                  api.closeWin();
              });

              api.addEventListener({
                  name: 'keyback'
              }, function(ret, err) {
                if(that.isOpenFrame){
                  that.isOpenFrame = false;
                  //这里写上你需要关闭的window或是frame
                  api.closeFrame({
                    name: 'shopSend',
                      animation: {
                          type: 'push',
                          subType: "from_left",
                          duration: 300
                      },
                  });
                }else{
                  api.closeWin();
                }

              });
            },
            methods: {
                // alert(JSON.stringify(ret));
                openSearch: function() {
                    this.searchName = $("#search-input").val();
                    app.openWin('orderSearch', {
                        shopId: api.pageParam.shopId,
                        searchName: this.searchName
                    })
                },
                //--获取订单列表（包含订单数量）
                orderListGet: function(scrolltobottom) {
                    // var shopId = api.pageParam.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id
                    var that = this;

                    app.ajax(service['orderListGet'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    page: this.page,
                                    order_state: this.order_state
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                if (res.data != '') {
                                    console.log("数据：" + JSON.stringify(res.data));
                                    if (!scrolltobottom) {
                                        that.orderList = [];
                                    }
                                    that.orderList = that.orderList.concat(res.data.list);
                                    // console.log(JSON.stringify("数据："+res.data.list+"类型："+res.data.list.constructor+"长度："+res.data.list.length));

                                    if (res.data.list.length > 0) {
                                        $(".shop_order_null").css("display", "none");
                                        $(".shop_order_list").css("display", "block");
                                    } else {
                                        if (!scrolltobottom) {
                                            $(".shop_order_null").css("display", "block");
                                            api.toast({
                                                msg: '该状态下没有任何订单',
                                                duration: 2000,
                                                location: 'middle'
                                            });
                                        } else {
                                            if (that.page != 1) {
                                                that.page = parseInt(that.page - 1);
                                                api.toast({
                                                    msg: '没有更多数据...',
                                                    duration: 2000,
                                                    location: 'middle'
                                                });
                                            }
                                        }

                                    }
                                    // console.log(that.orderList.constructor);
                                    that.orderNumber.unpay_num = res.data.unpay_num;
                                    that.orderNumber.pay_num = res.data.pay_num;
                                    that.orderNumber.express_num = res.data.express_num;
                                    that.orderNumber.refund_num = res.data.refund_num;
                                    that.orderNumber.comment_num = res.data.comment_num;
                                    that.orderNumber.finish_num = res.data.finish_num;

                                    console.log("获取商品列表成功!");

                                } else if (res.data == '') {
                                    that.orderList = [];
                                    api.toast({
                                        msg: '该状态下没有任何订单',
                                        duration: 2000,
                                        location: 'middle'
                                    });
                                }


                            }
                        });
                },
                //--状态切换
                upOrderTab: function(indes) {
                    var that = this;

                    if (indes == '-1') {
                        return;
                    } else if (indes == 0) { //--进行中
                        $(".shop_order_finish").css("display", "none");
                        $(".shop_order_await").css("display", "flex");

                        $(".shop_order_list").css("padding-top", "11rem");
                        $(".shop_order_null").css("padding-top", "13rem");
                        // $(".order_list_btn_del").css("display", "none");

                        this.activeIndex = 0; //点击“进行中”的时候默认选中待付款
                        this.order_state = 1; //点击“进行中”的时候默认获取待付款订单
                    } else if (indes == 1) { //--已完成
                        $(".shop_order_finish").css("display", "flex");
                        $(".shop_order_await").css("display", "none");

                        $(".shop_order_list").css("padding-top", "11rem");
                        $(".shop_order_null").css("padding-top", "13rem");
                        this.activeIndex = 4; //点击“已完成”的时候默认选中待评价
                        this.order_state = 4; //点击“已完成”的时候默认获取待评价订单
                    } else if (indes == 2) { //--已关闭
                        $(".shop_order_finish").css("display", "none");
                        $(".shop_order_await").css("display", "none");

                        //--已关闭，隐藏订单状态统计框
                        $(".shop_order_list").css("padding-top", "7.5rem");
                        $(".shop_order_null").css("padding-top", "12rem");
                        this.order_state = 6; //点击“已关闭”的时候获取已关闭订单
                    } else if (indes == 3) { //--已退款/退货
                        $(".shop_order_finish").css("display", "none");
                        $(".shop_order_await").css("display", "none");

                        //--已退款/退货，隐藏订单状态统计框
                        $(".shop_order_list").css("padding-top", "7.5rem");
                        $(".shop_order_null").css("padding-top", "12rem");

                        this.order_state = 8; //点击“已关闭”的时候获取已关闭订单
                    }
                    this.activeUpIndex = indes;
                    this.page = 1;

                    //--获取已关闭订单列表
                    var scrolltobottom = 0;
                    this.orderListGet(scrolltobottom);
                    window.scrollTo(0, 0); //--回到顶部
                },
                //--状态切换
                orderTab: function(indes) {
                    var that = this;
                    if (indes == '-1') {
                        return;
                    } else if (indes == 0) {
                        this.order_state = 1;
                    } else if (indes == 1) {
                        this.order_state = 2;
                    } else if (indes == 2) {
                        this.order_state = 3;
                    } else if (indes == 3) {
                        this.order_state = 7;
                    } else if (indes == 4) {
                        this.order_state = 4;
                    } else if (indes == 5) {
                        this.order_state = 5;
                    }
                    this.page = 1;
                    this.activeIndex = indes;

                    //--改变orderList的数据
                    var scrolltobottom = 0;
                    this.orderListGet(scrolltobottom);
                    window.scrollTo(0, 0); //--回到顶部
                },
                //--订单详情
                getOrderDetail: function(order_id) {
                    var that = this;
                    // app.openWin('shopOrderDetail', {order_id: order_id, shopId: this.shopId});

                    api.sendEvent({
                        name: 'getFrameName',
                        extra: {
                            frameName: 'shopSend',
                        }
                    });

                    this.isOpenFrame = true;

                    api.openFrame({
                        name: 'shopSend',
                        url: '../order_detail/order_detail.html',
                        rect: {
                            x: 0,
                            y: 0,
                            w: 'auto',
                            h: 'auto'
                        },
                        pageParam: {
                            shopId: that.shopId,
                            order_id: order_id
                        },
                        reload: true
                    });
                },

                //--底下按钮点击
                orderModBtn: function(mod, order_id, user_info) {
                    var that = this;
                    var unionid = $api.getStorage('shop_unionid');

                    switch (mod) {
                        case "shopChat":
                            console.log(JSON.stringify(user_info));
                            app.openWin('shopChat', {
                                otherId: user_info.openid, //id
                                nickname: user_info.nickname, //昵称
                                src: user_info.avater, //头像
                                new_nickname: user_info.new_nickname, //备注
                                openid2: user_info.openid
                            }, {
                                reload: true,
                            });
                            break;
                        case "shopSend":
                            //--发货
                            // app.openWin('shopSend', {shopId: that.shopId, order_id: order_id});
                            console.log('shopId: ' + api.pageParam.shopId)
                            that.isOpenFrame = true;
                            api.openFrame({
                                name: 'shopSend',
                                url: '../shop_send/shop_send.html',
                                rect: {
                                    x: 0,
                                    y: 0,
                                    w: 'auto',
                                    h: 'auto'
                                },
                                pageParam: {
                                    shopId: api.pageParam.shopId,
                                    order_id: order_id
                                },
                                reload: true
                            });
                            break;
                        case "shopDel":
                            //--确认是否删除订单
                            $('.tc_box').css("display", "block");
                            that.tem_order_id = order_id;
                            break;
                        case "shopFlow":
                            //--查看物流
                            app.openWin('shopFlow', {
                                shopId: that.shopId,
                                order_id: order_id
                            });
                            break;
                        case "shopRefundRefuse":
                            //--商家拒绝退款
                            app.openWin('shopRefundRefuse', {
                                shopId: that.shopId,
                                order_id: order_id
                            });
                            break;
                        case "shopRefundPass":
                            //--获取退款金额
                            app.ajax(service['orderDetailGet'], {
                                    data: {
                                        values: {
                                            unionid: unionid,
                                            order_id: order_id
                                        }
                                    }
                                },
                                function(res) {
                                    if (res.code) {
                                        alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                                    } else {
                                        console.log("获取订单详情成功");
                                        console.log(JSON.stringify(res.data));
                                        that.refundPrice = res.data.refund.price;
                                        that.yzc_status = res.data.yzc_status;
                                        that.order_return_id = res.data.refund.order_return_id;
                                        //--商家确认退款提示框
                                        $('.shop_order_refund').css("display", "block");
                                    }
                                });
                            break;
                        case "shopReturnRefuse":
                            //--商家拒绝退货
                            app.openWin('shopReturnRefuse', {
                                shopId: that.shopId,
                                order_id: order_id
                            });
                            break;
                        case "shopReturnconfirm":
                            //--商家同意申请退款但此时并不退款到买家
                            dialog.alert({
                                title: "通过申请提示",
                                msg: '是否通过退货退款申请',
                                buttons: ['取消', '确定']
                            }, function(ret) {
                                // alert(JSON.stringify(ret));
                                if (ret.buttonIndex == 2) { //  确认
                                    //--获取退货退款订单ID
                                    app.ajax(service['orderDetailGet'], {
                                            data: {
                                                values: {
                                                    unionid: unionid,
                                                    order_id: order_id
                                                }
                                            }
                                        },
                                        function(res) {
                                            if (res.code) {
                                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                                            } else {
                                                console.log("获取订单详情成功");
                                                console.log(JSON.stringify(res.data));
                                                that.order_return_id = res.data.refund.order_return_id;
                                                //--确认退货
                                                that.passReturn();
                                            }
                                        });


                                }
                            });
                            break;
                        case "shopReturnPass":
                            //--获取退货退款金额
                            app.ajax(service['orderDetailGet'], {
                                    data: {
                                        values: {
                                            unionid: unionid,
                                            order_id: order_id
                                        }
                                    }
                                },
                                function(res) {
                                    if (res.code) {
                                        alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                                    } else {
                                        console.log("获取订单详情成功");
                                        console.log(JSON.stringify(res.data));
                                        that.refundPrice = res.data.refund.price;
                                        that.yzc_status = res.data.yzc_status;
                                        that.order_return_id = res.data.refund.order_return_id;
                                        // --商家确认退货提示框
                                        $('.shop_order_return').css("display", "block");
                                    }
                                });

                            break;
                        default:
                            break;
                    }
                },
                //--取消删除订单
                delOrderCancel: function(et) {
                    $('.tc_box').css("display", "none");
                },
                //--确定删除订单
                delOrderSure: function() {
                    var that = this;
                    var unionid = $api.getStorage('shop_unionid');


                    app.ajax(service['shopDelOrder'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    order_id: that.tem_order_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                console.log(res.data);
                                // api.toast({
                                // 		msg: '订单已删除',
                                // 		duration: 2000,
                                // 		location: 'middle'
                                // });
                                alert('订单已删除')
                                that.tem_order_id = '';
                                $('.tc_box').css("display", "none");
                                var scrolltobottom = 0;
                                that.orderListGet(scrolltobottom);

                                api.sendEvent({
                                    name: 'close_win_refresh',
                                    extra: {
                                        shopId: api.pageParam.shopId,
                                    }
                                });
                            }
                        });
                },
                //------------------------------------
                //--关闭确认退款确认框(点击模态框事件)
                close_shop_order_refund: function() {
                    $('.shop_order_refund').css("display", "none");
                },
                //--关闭确认退款确认框(点击取消按钮)
                closeRefund: function() {
                    $('.shop_order_refund').css("display", "none");
                },
                //--确认退款事件
                passRefund: function() {
                    var that = this;

                    var unionid = $api.getStorage('shop_unionid');
                    var order_return_id = this.order_return_id;

                    app.ajax(service['agreeReturnOrder'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    order_return_id: order_return_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                console.log("确认退款成功");
                                console.log(JSON.stringify(res.data));
                                //--关闭确认退款提示框
                                $('.shop_order_refund').css("display", "none");

                                var scrolltobottom = 0;
                                that.orderListGet(scrolltobottom);
                            }
                        });
                },
                //------------------------------------
                //--关闭确认退货确认框(点击模态框事件)
                close_shop_order_return: function() {
                    $('.shop_order_return').css("display", "none");
                },
                //--关闭确认退货确认框(点击取消按钮)
                closeReturn: function() {
                    $('.shop_order_return').css("display", "none");
                },
                //--确认退货事件
                passReturn: function() {
                    var that = this;

                    var unionid = $api.getStorage('shop_unionid');
                    var order_return_id = this.order_return_id;

                    app.ajax(service['agreeReturnOrder'], {
                            data: {
                                values: {
                                    unionid: unionid,
                                    order_return_id: order_return_id
                                }
                            }
                        },
                        function(res) {
                            if (res.code) {
                                alert("错误代码code：" + res.code + "\n错误信息：" + res.message)
                            } else {
                                console.log("确认退货成功");
                                console.log(JSON.stringify(res.data));
                                //--关闭确认退货提示框
                                $('.shop_order_return').css("display", "none");

                                var scrolltobottom = 0;
                                that.orderListGet(scrolltobottom);
                            }
                        });
                },
                child_orderTab: function(inds) {
                    var that = this;
                    var orderTab = that.orderTab(inds);
                    api.execScript({
                        frameName: 'frame1',
                        script: orderTab
                    });
                },
                // 触底
                scrolltobottom: function() {
                    var that = this;
                    this.page = parseInt(this.page + 1);
                    var scrolltobottom = 1;

                    this.orderListGet(scrolltobottom);
                }
            },
            created: function() {
                var that = this;
                this.shopId = api.pageParam.shopId;
                console.log('created shopId: ' + api.pageParam.shopId);

                var scrolltobottom = 0;
                this.orderListGet(scrolltobottom);
                console.log('from created');

                api.addEventListener({
                    name: 'close_win_refresh',
                }, function(ret, err) {
                    that.shopId = ret.value.shopId;
                    var scrolltobottom = 0;
                    that.orderListGet(scrolltobottom);
                });



                api.addEventListener({
                    name: 'scrolltobottom',
                    extra: {
                        threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
                    }
                }, function(ret, err) {
                    that.scrolltobottom()
                });


                //下拉刷新
                app.refreshHeaderInfo(function() {
                    that.page = 1;
                    that.orderListGet();
                });
            }
        };

        var even = function() {

        };
        var newVue = new Vue(vm);
        even();

    })
}
